<?php
/**
 * 文章管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;

class UserActionModel extends Model
{
    const SHOW_STATUS=1; //已读
    const HIDE_STATUS=0; //未读

    protected $autoWriteTimestamp = true;

    public function user_car()
    {
        return $this->hasOne('UserCarModel', 'id', 'link_id')->field('id,car_id,series_id,price,mileage,card_time');
    }

    /**
     * 所有推荐
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getALL($user_id,$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('user_car')
            ->where('user_id',$user_id)
            ->where('type',0)
            ->group('link_id')
            ->order('status asc,create_time desc')
            ->paginate($pageSum);

        return $list;
    }


    /**
     * 删除
     * @param $user_id 用户ID
     * @param $link_id 关联ID 汽车或文章
     * @param int $type 类型：0 汽车，1 文章
     * @param $cat 分类：收藏 collect，点赞 good，留言 msg
     * @param int $msg_id 留言ID
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function del($user_id,$link_id,$type=0,$cat,$msg_id=0){

        $status=self::where('user_id',$user_id)
            ->where('link_id',$link_id)
            ->where('type',$type)
            ->where('cat',$cat)
            ->where('msg_id',$msg_id)
            ->delete();
        if($status){
            if($type==0){
                $userCar=UserCarModel::find($link_id);

                if($cat=='collect'){
                    $userCar->collect_count=$userCar->collect_count-1;
                }
                $userCar->save();
            }

            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * @param $user_id 用户ID
     * @param $link_id 关联ID 汽车或文章
     * @param int $type 类型：0 汽车，1 文章
     * @param $cat 分类：收藏 collect，点赞 good，留言 msg
     * @param int $msg_id 留言ID
     * @return UserAction
     */
    public static function adds($user_id,$link_id,$type=0,$cat,$msg_id=0){

        $data=[
            'user_id'=>$user_id,
            'link_id'=>$link_id,
            'type'=>$type,
            'cat'=>$cat,
            'create_time'=>time(),
            'msg_id'=>$msg_id,
            'status'=>0
        ];

        $status=self::create($data);

        if($status){
            if($type==0){
                $userCar=UserCarModel::find($link_id);

                if($cat=='collect'){
                    $userCar->collect_count=$userCar->collect_count+1;
                }
                if($cat=='msg'){
                    $userCar->msg_count=$userCar->msg_count+1;
                }
                $userCar->save();
            }
        }

        return $status;
    }

    /**
     * 检车数据是否存在
     * @param $user_id
     * @param $link_id
     * @param int $type
     * @param int $msg_id
     * @param string $cat
     * @return float|string
     */
    public static function checkGood($user_id,$link_id,$type=0,$msg_id=0,$cat='good'){
        $count=self::where('user_id',$user_id)
            ->where('link_id',$link_id)
            ->where('type',$type)
            ->where('cat',$cat)
            ->where('msg_id',$msg_id)
            ->count();

        return $count;
    }

} 